home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Risc World 3
/
Risc World 3.iso
/
SOFTWARE
/
ISSUE6
/
PD
/
PDF
/
pdf
/
c++
/
UriDispatch
< prev
Wrap
Text File
|
2003-02-14
|
5KB
|
159 lines
//--------------------------------------------------------------------------
//
// Copyright (c) 2002, Colin Granville
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// * The name Colin Granville may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//
//--------------------------------------------------------------------------
#include "UriDispatch.h"
#include "swis.h"
#include <stdlib.h>
#include <ctype.h>
#include "GuiTargets.h"
class UriHandler
{
public:
UriHandler();
~UriHandler();
bool dispatch(const string& uri);
private:
int my_ref;
GUI_DECLARE_EVENT_TARGETS(UriHandler);
GuiWimpTarget ant_uri_failed_target;
Claim ant_uri_failed(GuiWimpPollBlock&,const GuiIdBlock&);
};
//*************************************************************************
union AntStringValue
{
char *ptr;
int offset;
};
//*************************************************************************
struct AntURLMessageBlock : public GuiWimpMessageBase
{
union
{
char url[236];
struct
{
int tag;
AntStringValue url;
int flags;
AntStringValue body_file;
AntStringValue target;
AntStringValue body_mimetype;
} indirect;
} data;
};
//*************************************************************************
UriHandler::UriHandler()
: my_ref(-1),
ant_uri_failed_target(0,GuiWimp_EUserMessageAcknowledge,this,UriHandler::ant_uri_failed)
{
}
//*************************************************************************
UriHandler::~UriHandler()
{
}
//*************************************************************************
bool UriHandler::dispatch(const string& uri_string)
{
string uri=uri_string;
int i;
for (i=0;i<uri.size() && !isalpha(uri[i]);i++); //empty loop
uri.erase(0,i);
if (!uri.size()) return 0;
for (i=uri.size()-1;!(isalnum(uri[i]) || uri[i]=='/' || uri[i]=='-' || uri[i]=='_');i--); //empty loop
uri.resize(i+1);
if (uri.find("://")!=string::npos) {/*empty*/}
else if (uri.compare(0,4,"www.")==0) uri.insert(0,"http://");
else if (uri.compare(0,4,"ftp.")==0) uri.insert(0,"ftp://");
else if (uri.compare(0,7,"mailto:")==0) {/*empty*/}
else if (uri.compare(0,5,"news:")==0) {/*empty*/}
else if (uri.compare(0,5,"nntp:")==0) {/*empty*/}
else if (uri.find('@')!=string::npos) uri.insert(0,"mailto:");
else return 0;
if (!_swix(0x4e381 /*Acorn URI_Dispatch*/,_INR(0,2),0,uri.c_str(),0)) return 1;
AntURLMessageBlock mess;
mess.hdr.size=0;//keep compiler quiet
mess.copyString(mess.data.url,uri.c_str());
my_ref=mess.sendRecorded(0x4af80); //AntUrlMessage
return 1;
}
//*************************************************************************
Claim UriHandler::ant_uri_failed(GuiWimpPollBlock& wpb,const GuiIdBlock&)
{
if (wpb.userMessageAcknowledge.hdr.myRef!=my_ref) return DONT_CLAIM;
my_ref=-1;
AntURLMessageBlock& mess=(AntURLMessageBlock&) wpb;
int i;
for (i=0;isalpha(mess.data.url[i]);i++);//empty loop
string var;
var="Alias$URLOpen_";
var.append(mess.data.url,i);
if (getenv(var.c_str()))
{
var.erase(0,6);
var+=' ';
var+=mess.data.url;
_swix(Wimp_StartTask,_IN(0),var.c_str());
}
return CLAIM;
}
//*************************************************************************
bool dispatch_uri(const string& uri)
{
static UriHandler h;
return h.dispatch(uri);
}